import com.zh.mycommon.util.MyIdGenerator;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.name.Rename;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.http.MediaType;

import java.io.File;
import java.io.IOException;

import static com.zh.mycommon.util.MyFileUtil.DATA_MB;

/**
 * @author Zhou
 * @date 16:03 2025/4/30
 */
public class ThumbnailsDemo {
    public static void main(String[] args) throws IOException {
//        System.out.println(laterType.getSubtype());
//        UnCompressed(new File("C:\\Users\\Goddess\\Desktop\\Dashboard的UI.jpeg"));
//        UnCompressed(new File("C:\\Users\\Goddess\\Desktop\\Snipaste_2025-04-19_21-31-41.png"));

        for (int sizeM=0;sizeM<25;sizeM++){
            System.out.println(sizeM/20f);
            float quality = Math.max(1 - sizeM/20f,0.6f);
            System.out.println("quality = "+quality);
        }


    }

    //图片统一转为jpg格式
    static MediaType laterType = MediaType.IMAGE_JPEG;

    static void avatar(File file) throws IOException {
        var sizeM = file.length()/DATA_MB;

        //图片size<1m时,压缩头像质量为1，逐级递减为0；size>10m时,压缩头像质量为0.6
        float quality = Math.max(1 - sizeM/10f,0.6f);

        Thumbnails.of(file)
                .crop(Positions.CENTER)
                .size(512, 512)
                .outputQuality(quality)
                .outputFormat(laterType.getSubtype())
                .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
    }

    static void Compressed(File file) throws IOException {
        var sizeM = file.length()/DATA_MB;

        //图片size<1m时,压缩头像质量为1，逐级递减为0；size>10m时,压缩头像质量为0.6
        float quality = Math.max(1 - sizeM/10f,0.6f);

        Thumbnails.of(file)
                .scale(1)
                .outputQuality(quality)
                .outputFormat(laterType.getSubtype())
                .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
    }

    static void UnCompressed(File file) throws IOException {
        var sizeM = file.length()/DATA_MB;


        Thumbnails.of(file)
                .scale(1)
                .outputQuality(1)
                .outputFormat(laterType.getSubtype())
                .toFiles(Rename.PREFIX_DOT_THUMBNAIL);
    }
}
